﻿#include "safedoor.h"

#include "systemconfiguration.h"
#include "component_log.h"
#include "componentmanager.h"

#define STOP_TRIG ("0")
#define OTHER_TIG ("1")



SafeDoor::SafeDoor(QObject *parent)
	: ComponentBase(parent)
{
	m_name = "安全门组件";
	m_type = Component::component_safeDoor;
	m_state = { false, false, false, false,false };

	m_state.valied = false;;
}

SafeDoor::~SafeDoor()
{
}

// 组件初始化处理函数
int SafeDoor::ComponentInit()
{
	auto cfg = syscfg->GetSafeDoorSetting();
	if (!cfg.enable) {
		log_trace("安全门组件未使能，无需安全门组件");
		return COMMON_ERROR;
	}

	log_trace("安全门配置：急停：{}，复位：{}， 启动：{}， 请求进入：{}", cfg.stop, cfg.reset, cfg.start, cfg.enter);
	if (cfg.stop <= 0 || cfg.stop > 32 || cfg.reset <= 0 || cfg.reset > 32 ||
		cfg.start <= 0 || cfg.start > 32 || cfg.enter <= 0 || cfg.enter > 32) {
		SetErrmsg("安全门DI配置错误", false);
		return SERIOUS_ERROR;
	}

	m_setting.start = cfg.start;
	m_setting.stop = cfg.stop;
	m_setting.reset = cfg.reset;
	m_setting.enter = cfg.enter;

	auto rob = component->GetComponent(Component::component_robot);
	if (rob != nullptr) {
		QByteArray diInfo;
		rob->ExcuteCmd((int)RobotCmdType::robot_cmd_get_di, QString::number(-1).toUtf8(), diInfo);
		SetDIInfo(diInfo);
		rob->RegisterSubscriberFunction((int)RobotMsgType::robot_msg_DI, [&](const QByteArray& info) {
			return SetDIInfo(info);
		});
	} else {
		SetErrmsg("获取机械手组件出错，安全门组件初始化异常", false);
		return SERIOUS_ERROR;
	}

	return 0;
}
/*自检接口*/
bool SafeDoor::ComponentCheckSelfState()
{
	if (!m_state.valied) {
		log_error("safe door state is not valied");
		SetErrmsg("未获取到有效的安全门信息", false);
		return false;
	}

	if (m_state.stop) {
		SetErrmsg("急停按钮被按下", false);
		return false;
	}

	if (m_state.enter) {
		SetErrmsg("请求进入按钮触发中", false);
		return false;
	}

	return true;
}
/*复位接口*/
bool SafeDoor::ComponentSelfReset()
{
	if (m_state.stop) {
		SetErrmsg("急停按钮被按下, 无法复位", false);
		return false;
	}

	if (m_state.enter) {
		SetErrmsg("请求进入按钮触发中，无法复位", false);
		return false;
	}

	return true;
}

void SafeDoor::ResponseSystemState(int state)
{

}

int SafeDoor::SetDIInfo(const QString& info)
{
	QString entry = info.at(m_setting.enter - 1);
	TrigEntry(entry == OTHER_TIG);

	QString reset = info.at(m_setting.reset - 1);
	TrigReset(reset == OTHER_TIG);

	QString stop = info.at(m_setting.stop - 1);
	TrigStop(stop == STOP_TRIG);

	QString start = info.at(m_setting.start - 1);
	TrigStart(start == OTHER_TIG);

	return 0;
}
void SafeDoor::TrigStop(bool flag)
{
	m_state.valied = true;
	if (flag == m_state.stop) {
		return;
	}
	m_state.stop = flag;

	if (m_state.stop) {
		SetErrmsg("急停按钮触发，系统停止运行");
	} else {
		SetWarningmsg("急停解除");
	}

}
void SafeDoor::TrigReset(bool flag)
{
	if (flag == m_state.reset) {
		return;
	}
	m_state.reset = flag;
	if (flag) {
		SetWarningmsg("复位按钮触发");
		emit PublishSystemEvent((int)m_type, (int)ComponentEvent::system_event_reset);
	} else {
		log_error("reset trig over");
	}
	
}
void SafeDoor::TrigEntry(bool flag)
{
	if (flag == m_state.enter) {
		return;
	}
	m_state.enter = flag;
	if (flag) {
		SetWarningmsg("请求进入按钮触发");
		emit PublishSystemEvent((int)m_type, (int)ComponentEvent::system_event_Enter);
	} else {
		SetWarningmsg("请求进入按钮结束");
	}
}
void SafeDoor::TrigStart(bool flag)
{
	if (flag == m_state.start) {
		return;
	}
	m_state.start = flag;
	if (flag) {
		SetWarningmsg("启动按钮触发");
		emit PublishSystemEvent((int)m_type, (int)ComponentEvent::system_event_start);
	} else {
		log_error("start trig over");
	}
}
